package com.younion.action.manage;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;

import com.younion.common.base.BaseAction;
import com.younion.common.constants.SysConstants;
import com.younion.common.util.ImageFileUtil;
import com.younion.util.UUIDUtil;

public class UploadAction extends BaseAction {
	private static final long serialVersionUID = -1L;

	private File uploadObj; // 上传文件
	private String uploadObjFileName; // 上传文件名

	public String uploadFavourableEntityImg() {
		String ext = this.uploadObjFileName.substring(this.uploadObjFileName
				.lastIndexOf("."));
		String realPath = this.request.getSession().getServletContext()
				.getRealPath(File.separator);
		// url：upload/images/card/temp
		this.uploadObjFileName = System.currentTimeMillis() + ext;
		File file = new File(realPath + File.separator
				+ SysConstants.FavourableEntity_IMG_TEMP_PATH);
		if (!file.exists()) {
			file.mkdirs();
		}
		File newFile = new File(file, this.uploadObjFileName);
		try {
			FileUtils.copyFile(this.uploadObj, newFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		int sub_index = (String.valueOf((float) newFile.length() / 1024))
				.indexOf(".");
		String show = ((float) newFile.length() / 1024 + "000").substring(0,
				sub_index + 2);
		request.setAttribute("fileSize", show);
		request.setAttribute("id", "temp");
		request.setAttribute("picname", this.uploadObjFileName);

		return SUCCESS;
	}

	public String uploadImgFile() {
		int width = ImageFileUtil.getImgWidth(this.uploadObj);
		int height = ImageFileUtil.getImgHeight(this.uploadObj);
		String alreadyPixels = request.getParameter("alreadyPixels");//已经上传的像素
		String pixels = request.getParameter("pixels");//需要上传的像素
		if (alreadyPixels != null && !alreadyPixels.trim().equals("")) {
			String alPixel[] = alreadyPixels.split(",");
			for (String temp : alPixel) {
				if ((width + "_" + height).equals(temp)) {
					this.uploadObjFileName = width + "_" + height;
					request.setAttribute("result", "already");//已存在的像素
					return SUCCESS;
				}
			}
		}
		boolean flag = false;
		String pixel[] = pixels.split(",");
		for (String temp : pixel) {
			if ((width + "_" + height).equals(temp)) {
				flag = true;
				break;
			}
		}
		if (!flag) {
			this.uploadObjFileName = width + "_" + height;
			request.setAttribute("result", "noNeed");//不在需要上传的像素中
			return SUCCESS;
		}
		String folder = request.getParameter("folder");
		ServletContext sc = this.request.getSession().getServletContext();
		String ext = this.uploadObjFileName.substring(this.uploadObjFileName.lastIndexOf("."));
		String realPath = sc.getRealPath(File.separator);
		// url：/upload/images/文件夹/temp/
		String servletPath = SysConstants.IMG_PATH + File.separator + folder + File.separator + "temp" + File.separator;
		String uuid = new UUIDUtil().getUuid().toString();
		this.uploadObjFileName = "_" + width + "_" + height + "_" + uuid + System.currentTimeMillis() + ext;
		File file = new File(realPath + File.separator + servletPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		File newFile = new File(file, this.uploadObjFileName);
		try {
			FileUtils.copyFile(this.uploadObj, newFile);
		} catch (IOException e) {
			e.printStackTrace();
			request.setAttribute("result", "error");
			return SUCCESS;
		}
		int sub_index = (String.valueOf((float) newFile.length() / 1024)).indexOf(".");
		String show = ((float) newFile.length() / 1024 + "000").substring(0, sub_index + 2);
		request.setAttribute("fileSize", show);
		request.setAttribute("result", "success");
		return SUCCESS;
	}

	public String uploadCardPic() {
		String ext = this.uploadObjFileName.substring(this.uploadObjFileName
				.lastIndexOf("."));
		String realPath = this.request.getSession().getServletContext()
				.getRealPath(File.separator);
		// url：upload/images/card/temp
		this.uploadObjFileName = System.currentTimeMillis() + ext;
		File file = new File(realPath + File.separator
				+ SysConstants.CARD_IMG_TEMP_PATH);
		if (!file.exists()) {
			file.mkdirs();
		}
		File newFile = new File(file, this.uploadObjFileName);
		try {
			FileUtils.copyFile(this.uploadObj, newFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		int sub_index = (String.valueOf((float) newFile.length() / 1024))
				.indexOf(".");
		String show = ((float) newFile.length() / 1024 + "000").substring(0,
				sub_index + 2);
		request.setAttribute("fileSize", show);
		request.setAttribute("id", "temp");
		request.setAttribute("picname", this.uploadObjFileName);
		return SUCCESS;
	}
	
	public String uploadActiveImg() {
		int width = ImageFileUtil.getImgWidth(this.uploadObj);
		int height = ImageFileUtil.getImgHeight(this.uploadObj);
		String uuid = new UUIDUtil().getUuid().toString();
		ServletContext sc = this.request.getSession().getServletContext();
		String ext = this.uploadObjFileName.substring(this.uploadObjFileName.lastIndexOf("."));
		String realPath = sc.getRealPath(File.separator);
		// url：upload/images/active/temp
		String servletPath = SysConstants.ACTIVE_IMG_PATH + File.separator + "temp";
		this.uploadObjFileName = uuid + System.currentTimeMillis() + ext;
		File file = new File(realPath + File.separator + servletPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		File newFile = new File(file, this.uploadObjFileName);
		try {
			FileUtils.copyFile(this.uploadObj, newFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		int sub_index = (String.valueOf((float) newFile.length() / 1024)).indexOf(".");
		String show = ((float) newFile.length() / 1024 + "000").substring(0, sub_index + 2);
		request.setAttribute("fileSize", show);
		request.setAttribute("uuid", uuid);
		this.uploadObjFileName = width + "_" + height + "_" + this.uploadObjFileName;
		return SUCCESS;
	}
	
	public String uploadWebIndexPic() {
		ServletContext sc = this.request.getSession().getServletContext();
		String ext = this.uploadObjFileName.substring(this.uploadObjFileName.lastIndexOf("."));
		String realPath = sc.getRealPath(File.separator);
		String folder = request.getParameter("folder");
		if (folder == null || folder.equals("")) {
			folder = "webIndexPic";
			this.uploadObjFileName = System.currentTimeMillis() + ext;
		}
		// url：upload/images/webIndexPic/temp
		String servletPath = SysConstants.IMG_PATH + File.separator + folder + File.separator + "temp";			
		File file = new File(realPath + File.separator + servletPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		File newFile = new File(file, this.uploadObjFileName);
		try {
			FileUtils.copyFile(this.uploadObj, newFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		int sub_index = (String.valueOf((float) newFile.length() / 1024)).indexOf(".");
		String show = ((float) newFile.length() / 1024 + "000").substring(0, sub_index + 2);
		request.setAttribute("fileSize", show);
		return SUCCESS;
	}

	public File getUploadObj() {
		return uploadObj;
	}

	public void setUploadObj(File uploadObj) {
		this.uploadObj = uploadObj;
	}

	public String getUploadObjFileName() {
		return uploadObjFileName;
	}

	public void setUploadObjFileName(String uploadObjFileName) {
		this.uploadObjFileName = uploadObjFileName;
	}

}